package com.justnow.offer;


import java.util.LinkedList;
import java.util.Stack;

public class Solution27 {

    /**
     * 方法一：DFS的方法，时间复杂度O(N)，N为栈中节点的个数
     * @param root
     * @return
     */
    public TreeNode mirrorTree(TreeNode root) {
        if (root == null)
            return root;
        TreeNode temp = root.left;
        root.left = mirrorTree(root.right);
        root.right = mirrorTree(temp);
        return root;
    }

    /**
     * 方法二，DFS，使用栈
     * @param root
     * @return
     */
    public TreeNode mirrorTree2(TreeNode root) {
        if (root == null) return null;
        Stack<TreeNode> stack = new Stack<>();
        stack.add(root);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            //if (node == null) continue;
            if (node.left != null) stack.add(node.left);
            if (node.right != null) stack.add(node.right);
            TreeNode tmp = node.left;
            node.left = node.right;
            node.right = tmp;
        }
        return root;
    }


}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    public TreeNode(int val) {
        val = val;
    }
}